package cn.stylefeng.guns.modular.business.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.RecoverConst;
import cn.stylefeng.guns.modular.business.entity.UserAccount;
import cn.stylefeng.guns.modular.business.mapper.UserAccountMapper;
import cn.stylefeng.guns.modular.business.model.params.UserAccountParam;
import cn.stylefeng.guns.modular.business.model.result.UserAccountResult;
import cn.stylefeng.guns.modular.business.service.UserAccountService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.swing.text.html.Option;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 用户账户 服务实现类
 * </p>
 *
 * @author pzp
 * @since 2019-08-04
 */
@Service
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

    @Override
    public void add(UserAccountParam param) {
        UserAccount entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(UserAccountParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(UserAccountParam param) {
        UserAccount oldEntity = getOldEntity(param);
        UserAccount newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public UserAccountResult findBySpec(UserAccountParam param) {
        return null;
    }

    @Override
    public List<UserAccountResult> findListBySpec(UserAccountParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(String userName, String payMode) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, userName, payMode);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(UserAccountParam param) {
        return param.getPayId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private UserAccount getOldEntity(UserAccountParam param) {
        return this.getById(getKey(param));
    }

    private UserAccount getEntity(UserAccountParam param) {
        UserAccount entity = new UserAccount();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    /**
     * 添加账号
     *
     * @param id      账户id
     * @param userId  用户id
     * @param payMode 交易方式：0：支付宝，1：微信，2：银行卡
     * @param account 账号
     * @param bank    开户行
     */
    @Override
    public void addOrUpdate(Integer id, String realName, Long userId, Integer payMode, String account, String bank) {
        //账号去重校验
        UserAccount checkUserAccount = this.getOne(new QueryWrapper<UserAccount>().eq("delete_flag", RecoverConst.DEL_FLAG_FALSE).eq("pay_mode",payMode).eq("user_id",userId).eq("account", account));
        UserAccount userAccount;
        if (null != id) {
            if (null != checkUserAccount & !checkUserAccount.getPayId().equals(id)) {
                throw new ServiceException(400,"账号已存在");
            }
            userAccount = Optional.ofNullable(this.getById(id)).orElseThrow(() -> new ServiceException(BizExceptionEnum.DB_RESOURCE_NULL));

        } else {
            if (null!=checkUserAccount){
                throw new ServiceException(400,"账号已存在");
            }
            userAccount = new UserAccount();
            userAccount.setDeleteFlag(RecoverConst.DEL_FLAG_FALSE);
            userAccount.setCreateTime(new Date());
        }
        userAccount.setRealName(realName);
        userAccount.setUserId(userId);
        userAccount.setPayMode(payMode);
        userAccount.setAccount(account);
        userAccount.setBank(bank);
        userAccount.setUpdateTime(new Date());
        this.saveOrUpdate(userAccount);
    }

    /**
     * 删除账户
     *
     * @param id 账户id
     */
    @Override
    public void delete(Integer id) {
        UserAccount userAccount = new UserAccount();
        userAccount.setPayId(id);
        userAccount.setUpdateTime(new Date());
        userAccount.setDeleteFlag(RecoverConst.DEL_FLAG_TRUE);
        this.updateById(userAccount);
    }

    /**
     * 账号列表
     *
     * @param userId  用户id
     * @param payMode 交易方式：0：支付宝，1：微信，2：银行卡
     * @return
     */
    @Override
    public List<UserAccount> list(Long userId, Integer payMode) {
        return this.list(new QueryWrapper<UserAccount>().eq("delete_flag", RecoverConst.DEL_FLAG_FALSE).eq("pay_mode", payMode).eq("user_id", userId));
    }
}
